package ShareYourWay.webpages;

import javax.swing.text.SimpleAttributeSet;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.extensions.markup.html.form.select.Select;
import org.apache.wicket.extensions.markup.html.form.select.SelectOption;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.EmailTextField;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.ListChoice;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import ShareYourWay.functions.App;
import ShareYourWay.functions.Functions_Meetup;
import ShareYourWay.functions.SignInSession;
import ShareYourWay.project.Meetup;
import ShareYourWay.project.University;
import ShareYourWay.project.User;
import ShareYourWay.util.AuthPage;

@AuthorizeInstantiation("USER")
public class Profile extends WebPage {
	
	SignInSession s = SignInSession.get();
	public User user = s.getUser();
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public Profile(){	
		
		final User u = new User();
		final Meetup meet = new Meetup();
		final University uni = new University();
		final Functions_Meetup fm = new Functions_Meetup();
		final App app = new App();
		
		final Component MeetPanel;
		MeetPanel=new PanelYourMeets("PanelYourMeets");
		MeetPanel.setOutputMarkupId(true); // Nos permite cargar el Panel con el boton AJAX
		add(MeetPanel);
		
		final Component MeetPanel2;
		MeetPanel2=new PanelMeetsOn("PanelAssistMeets");
		MeetPanel2.setOutputMarkupId(true); // Nos permite cargar el Panel con el boton AJAX
		add(MeetPanel2);
		
		BookmarkablePageLink<AuthPage> l = new BookmarkablePageLink<AuthPage>("homeLink", Meets.class);
		add(l);
		
		Form<User> form = new Form<User>("formProfile");
		
//		TextField<String> tname = new TextField<String>("tname", new PropertyModel<String>(u, "name"));
//		tname.setRequired(true);
//		TextField<String> tsurname = new TextField<String>("tsurname", new PropertyModel<String>(u, "surname"));
//		tsurname.setRequired(true);
//		EmailTextField tmail = new EmailTextField("tmail", new PropertyModel<String>(u, "mail"));
//		tmail.setRequired(true);
//		PasswordTextField tpassword = new PasswordTextField("tpassword", new PropertyModel<String>(u, "password"));
//		tpassword.setRequired(true);
		TextField<String> tphone = new TextField<String>("tphone", new PropertyModel<String>(u, "phone"));
		tphone.setRequired(true);
		TextField<String> tcity = new TextField<String>("tcity", new PropertyModel<String>(u, "city"));
		tcity.setRequired(true);
		ListChoice<String> tschool = new ListChoice<String>("tschool", new PropertyModel<String>(uni, "name"), app.listUniNames());
		tschool.setRequired(true);
		Select<Boolean> tcar = new Select<Boolean>("tcar", new PropertyModel<Boolean>(u, "car"));
		tcar.setRequired(true);
		Select<Boolean> tsex = new Select<Boolean>("tsex", new PropertyModel<Boolean>(u, "sex"));
		tsex.setRequired(true);
		
		add(form);
//		form.add(tname);
//		form.add(tsurname);
//		form.add(tmail);
//		form.add(tpassword);
		form.add(tphone);
		form.add(tcity);
		form.add(tschool);
		form.add(tcar);
		form.add(tsex);
		
		tcar.add(new SelectOption<Boolean>("s0", new Model<Boolean>(true)));
		tcar.add(new SelectOption<Boolean>("s1", new Model<Boolean>(false)));
		
		tsex.add(new SelectOption<String>("s0", new Model<String>("Female")));
		tsex.add(new SelectOption<String>("s1", new Model<String>("Male")));
		
		 //---------------------- Profile -------------------------
		
		Label name = new Label ("pname", new PropertyModel<String>(user, "name"));
		Label surname = new Label ("psurname", new PropertyModel<String>(user, "surname"));

		add(name);
		add(surname);
 
		
		 AjaxButton updateUser = new AjaxButton("updateProfile") {
				private static final long serialVersionUID = 1L;

				@Override
	            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
					University v = new University();
					System.out.println(uni.getName());
					v = app.getUniversityByName(uni.getName());
					u.setUniversity(v);
					u.setId(user.getId());
					u.setName(user.getName());
					u.setMail(user.getMail());
					u.setPassword(user.getPassword());
					u.setSurname(user.getSurname());
					u.setNick(user.getNick());
					
					System.out.println(u.getId() + ", " + u.getMail() + ", " + u.getUniversity().getIdUniversity() + ", " + u.getUniversity().getCity() + ", " + u.getPhone());
					app.updateProfile(u);
					target.appendJavaScript("alert('Usuario actualizado!');");
				}
		 };
		 
		 form.add(updateUser);
		 
		 Link h = new Link("home"){
				public void onClick(){
					setResponsePage(Meets.class);
				}
			};
			add(h);
			
		Link logout = new Link("logout"){
				public void onClick(){
					s.invalidateNow(); 
		             setResponsePage(HomePage.class);;
				}
			};
			add(logout);
	}
}
